<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 角色业务逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class RoleLogic extends BaseLogic {

    /**
     * 根据角色名称查询角色
     * @param   string  $rolename   角色名称
     * @return  array   $rolelist   角色列表
     * @author  derek <693472824@qq.com>
     */
    public function getRoleListByName ($rolename = null) {
        
        $arrlist = array();
        
        $roleModel = M('Role');
        $whererole['role_name'] = array(
                'like',
                '%' . (string)$rolename . '%'
        );
        
        $rolelist = $roleModel->where($whererole)->select();
        
        return $rolelist;
    }

    /**
     * 根据角色ID获取角色
     * @param   integer $roid 角色ID
     * @author  derek <693472824@qq.com>
     */
    public function getRoleByID ($role_id=0) {
        
        if(empty($role_id) || !is_numeric($role_id)){
            return null;
        }
        
        $roleModel = M('Role');
        $role_where["role_id"] = (string)$role_id;
        $role = $roleModel->where($role_where)->find();
        return $role;
    }
    
    /**
     * 保存用户信息
     *
     * @author derek <693472824@qq.com>
     */
    public function saveAction() {
        
        $roleID = I('role_id');
        $roleModel = D('Role');
    
        if ($roleModel->create()) {
    
            if (empty($roleID)) {
                return $roleModel->add();
            } else {
                $map['role_id'] = $roleID;
                return $roleModel->where($map)->save();
            }
        } else {
            $this->error = $roleModel->getError();
            return false;
        }
    }

    /**
     * 角色删除
     * @param   integer|array 需要删除的角色ID
     * @return  boolean
     * @author  derek <693472824@qq.com>
     */
    public function del ($role_ids) {
        
        if(empty($role_ids)){
            return false;
        }
        
        if(is_array($role_ids)){
            
            $where['role_id'] = array('in', $role_ids);
        }elseif (is_numeric($role_ids)){
            
            $where['role_id'] = trim((string)$role_ids, ',');
        }
        
        $result = M('Role')->where($where)->delete();
        
        return $result;
    }

    /**
     * 设置角色权限
     * @param  string   $roleid     角色ID
     * @param  array    $menu_tree  菜单树
     * @author derek <693472824@qq.com>
     */
    public function getAuthByRoleID($role_id) {
        
        $menu_tree = array();
        
        if(empty($role_id)){
            return $menu_tree;
        }
        
        $menuLogic = D('Menu', 'Logic');
        
        $menu_map = array();
        $menu_field = 'menu_id, menu_name, menu_url, parent_id';
        $menus = $menuLogic->getMenus($menu_map, $menu_field);
        
        $role_menu = $menuLogic->getMenuByRID($role_id, 'menu_id, menu_name');
        $role_menu = empty($role_menu) ? array() : $role_menu;
        $role_menu_ids = array_keys($role_menu);
        
        foreach ($role_menu_ids as $k=>$v){
            if($menus[$v]){
                $menus[$v]['class'] = 'checked';
            }
        }
        
        $menu_tree = api('Util/list_to_tree', array($menus, 'menu_id', 'parent_id', '_child', 0));
        
        return $menu_tree;
        
    }
    
    public function updateAuth($roleid=0, $menu_str=''){
        
        if(empty($roleid)){
            return false;
        }
        
        if(is_array($menu_str)){
            $menu_str = implode(',', $menu_str);
        }
        
        $menu_str = trim($menu_str, ',');
        $data['role_id'] = $roleid;
        $data['menu_id_str'] = $menu_str;
        
        $uid = AdminLogic::is_login();
        session('ADMIN_MENU' . $uid, null);
        
        return M('Role')->save($data);
        
    }
    
    /**
     * 根据角色ID查询角色
     * @param   string  $rolename   角色名称
     * @return  array   $rolelist   角色列表
     * @author  derek <693472824@qq.com>
     */
    public function getRoleListByID ($roleID = array()){
        
        $map = array();
        $rolelist=null;
        $roleID = is_string($roleID) ? trim($roleID, ',') : $roleID;
        
        if(!empty($roleID)){
            $map['role_id'] = array('in', $roleID);
            $roleModel = D('Role');
            $rolelist = $roleModel->where($map)->select();
        }
        
        return $rolelist;
    }
    
    
}